package com.shop.cloud.filter;
// package com.shop.cloud.filter;
//   
//  import java.security.MessageDigest;
//  import java.util.Calendar;
//  import java.util.Date;
//    import java.util.HashMap;
//  import java.util.Map;
//  import java.util.Map.Entry;
//  import java.util.concurrent.Executors;
//  import java.util.concurrent.ScheduledExecutorService;
//  import java.util.concurrent.TimeUnit;
//  
//  import org.apache.log4j.Logger;
// 
// 
// 
// public class TokenUtil {
//  
//      private static final int INTERVAL = 7;// token过期时间间隔 天
//      private static final String YAN = "testMRf1$789787aadfjkds//*-+'[]jfeu;384785*^*&%^%$%";// 加盐
//      private static final int HOUR = 3;// 检查token过期线程执行时间 时
//      
//      private static Logger logger = Logger.getLogger("visit");
//  
//      private static Map<Integer, Token> tokenMap = new HashMap<Integer, Token>();
//      private static TokenUtil tokenUtil = null;
//      static ScheduledExecutorService scheduler =Executors.newSingleThreadScheduledExecutor(); 
//  
//      static {
//          logger.info("\n===============进入TokenUtil静态代码块==================");
//          listenTask();
//     }
//      
//  
//      public static TokenUtil getTokenUtil() {
//          if (tokenUtil == null) {
//             synInit();
//         }
//  
//          return tokenUtil;
//     }
// 
//     private static synchronized void synInit() {
//          if (tokenUtil == null) {
//             tokenUtil = new TokenUtil();
//         }
//     }
// 
//     public TokenUtil() {
//      }
//      public static Map<Integer, Token> getTokenMap() {
//          return tokenMap;
//     }
// 
//     /**
//      * 产生一个token
//        */
//     public static Token generateToken(String uniq,int id) {
//          Token token = new Token(MD5(System.currentTimeMillis()+YAN+uniq+id), System.currentTimeMillis());
//        synchronized (tokenMap) {
//              tokenMap.put(id, token);
//        }
//          return token;
//     }
// 
//   
//       /**
//        * @Title: removeToken
//        * @Description: 去除token
//       * @param @param nonce
//        * @param @return 参数
//        * @return boolean 返回类型
//       */
//       public static boolean removeToken(int id) {
//           synchronized (tokenMap) {
//             tokenMap.remove(id);
//             logger.info(tokenMap.get(id) == null ? "\n=========已注销========": "\n++++++++注销失败+++++++++++++++");
//          }
//         return true;
//      }
//  
//       /**
// 86      * @Title: volidateToken
// 87      * @Description: 校验token
// 88      * @param @param signature
// 89      * @param @param nonce
// 90      * @param @return 参数
// 91      * @return boolean 返回类型
// 92      */
// 93     public static boolean volidateToken(String signature, int id) {
// 94         boolean flag = false;
// 95         Token token = (Token) tokenMap.get(id);
// 96         if (token != null && token.getSignature().equals(signature)) {
// 97             logger.info("\n=====已在线=======");
// 98             flag = true;
// 99         }
//100 
//101         return flag;
//102     }
//103     
//104     /**
//105      * 
//106      * @Title: MD5
//107      * @Description: 加密
//108      * @param @param s
//109      * @param @return 参数
//110      * @return String 返回类型
//111      */
//112     public final static String MD5(String s) {
//113         try {
//114             byte[] btInput = s.getBytes();
//115             // 获得MD5摘要算法的 MessageDigest 对象
//116             MessageDigest mdInst = MessageDigest.getInstance("MD5");
//117             // 使用指定的字节更新摘要
//118             mdInst.update(btInput);
//119             // 获得密文
//120             return byte2hex(mdInst.digest());
//121         } catch (Exception e) {
//122             e.printStackTrace();
//123             return null;
//124         }
//125     }
//126 
//127     /**
//128      * 将字节数组转换成16进制字符串
//129      * @param b
//130      * @return
//131      */
//132     private static String byte2hex(byte[] b) {
//133         StringBuilder sbDes = new StringBuilder();
//134         String tmp = null;
//135         for (int i = 0; i < b.length; i++) {
//136             tmp = (Integer.toHexString(b[i] & 0xFF));
//137             if (tmp.length() == 1) {
//138                 sbDes.append("0");
//139             }
//140             sbDes.append(tmp);
//141         }
//142         return sbDes.toString();
//143     }
//144     
//145     /**
//146     * @Title: listenTask 
//147     * @Description: 定时执行token过期清除任务
//148     * @param     参数
//149     * @return void    返回类型
//150      */
//151     public static void listenTask(){
//152         Calendar calendar = Calendar.getInstance();
//153         int year = calendar.get(Calendar.YEAR);
//154         int month = calendar.get(Calendar.MONTH);
//155         int day = calendar.get(Calendar.DAY_OF_MONTH);
//156         //定制每天的HOUR点，从明天开始
//157         calendar.set(year, month, day+1, HOUR, 0, 0);
//158        // calendar.set(year, month, day, 17, 11, 40);
//159         Date date = calendar.getTime();
//160         
//161         scheduler.scheduleAtFixedRate( new ListenToken(), (date.getTime()-System.currentTimeMillis())/1000, 60*60*24, TimeUnit.SECONDS);
//162     }
//163     
//164     
//165 
//166     /**
//167      * @ClassName: ListenToken
//168      * @Description: 监听token过期线程runnable实现
//169      * @author mrf
//170      * @date 2015-10-21 下午02:22:24
//171      * 
//172      */
//173     static class ListenToken implements Runnable {
//174         public ListenToken() {
//175             super();
//176         }
//177 
//178         public void run() {
//179             logger.info("\n**************************执行监听token列表****************************");
//180             try {
//181                 synchronized (tokenMap) {
//182                     for (int i = 0; i < 5; i++) {
//183                         if (tokenMap != null && !tokenMap.isEmpty()) {
//184                             for (Entry<Integer, Token> entry : tokenMap.entrySet()) {
//185                                 Token token = (Token) entry.getValue();
//186                                 logger.info("\n==============已登录用户有："+entry + "=====================");
//187 //                            try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}
//188                                 int interval = (int) ((System.currentTimeMillis() - token.getTimestamp()) / 1000 / 60 / 60 / 24);
//189                                 if (interval > INTERVAL) {
//190                                     tokenMap.remove(entry.getKey());
//191                                     logger.info("\n==============移除token：" + entry+ "=====================");
//192                                 }
//193 
//194                             }
//195                         }
//196                     }
//197                     
//198                 }
//199             } catch (Exception e) {
//200                 logger.error("token监听线程错误："+e.getMessage());
//201                 e.printStackTrace();
//202             }
//203         }
//204     }
//205     
//206      
//207 
//208     public static void main(String[] args) {
//209         System.out.println(generateToken( "s",1));
//210         System.out.println(generateToken( "q",1));
//211         System.out.println(generateToken( "s3",2));
//212         System.out.println(generateToken( "s4",3));
//213         System.out.println(removeToken(3));
//214         System.out.println(getTokenMap());
//215     }
//216 
//217 }
//218 
//219  